_debug = false;
var Util = {
	ParseUrl:function(url){
		if(typeof(url)=='undefined')
			url = window.location.toString();
		url = decodeURI(url);
		var pos = url.lastIndexOf("?");
		if(pos == -1 || url == '?') return {};
		url = url.slice(pos+1);
		var items = url.split("&");
		var str = "";
		for(var i=0;i<items.length;i++){
			var paire = items[i].split("=");
			str+=paire[0]+":\""+paire[1]+"\",";
		}
		str = str.substring(0,str.length-1);
		str ="({"+str+"})";
		return eval(str);
	},
	ToUrlRequest : function(obj){
		var str = "";
		for(var t in obj){
			str += t+'='+obj[t]+'&';
		}
		str = str.slice(0,-1);
		return str;
	},
	setCaretPos: function (ele, pos) {   
	    if(ele.createTextRange) {   
	        /* Create a TextRange, set the internal pointer to  
	           a specified position and show the cursor at this  
	           position  
	        */  
	        var range = ele.createTextRange();   
	        range.move("character", pos);   
	        range.select();   
	    } else if(ele.selectionStart >= 0) {   
	        /* Gecko is a little bit shorter on that. Simply  
	           focus the element and set the selection to a  
	           specified position  
	        */  
	        ele.focus();   
	        ele.setSelectionRange(pos, pos);
	    }   
	},
	insertAtCursor:function(myField, myValue){
	  //IE support
		if (document.selection){
		    myField.focus();
		    sel = document.selection.createRange();
		    sel.text = myValue;
		}
		  //MOZILLA/NETSCAPE support
		else if(myField.selectionStart || myField.selectionStart == '0'){
		    var startPos = myField.selectionStart;
		    var endPos = myField.selectionEnd;
		    myField.value = myField.value.substring(0, startPos)
		                  + myValue
		                  + myField.value.substring(endPos, myField.value.length);
		} else {
		    myField.value += myValue;
		}
	},
	goTop : function(){			
		window.scrollTo(0,0);
		document.body.scrollLeft=0;
		document.body.scrollTop=0;			
	}
	
}
String.prototype.trunc = function(count){
	var str = this.toString();
	str = str.substring(0,count);
	if(str.length<this.toString().length) str += "...";
	return str;
}
String.prototype.unescapeHTML = function(){
	var str = this.toString();
	str = str.replace(/&lt;/g,'<');
	str = str.replace(/&gt;/g,'>');
	str = str.replace(/&nbsp;/g,' ');
	str = str.replace(/<br \/>/gi,'\n');
	str = str.replace(/<br>/gi,'\n');
	return str;
}
String.prototype.escapeHTML = function(){
	var str = this.toString();
	str = str.replace(/</g,'&lt;');
	str = str.replace(/>/g,'&gt;');
	str = str.replace(/ /g,'&nbsp;');
	str = str.replace(/\t/g,'&nbsp;&nbsp;&nbsp;&nbsp;');
	str = str.replace(/\n/gi,'<br />');
	return str;
}
String.prototype.trim = function (){
	var str = this.toString();
	var len = str.length;
	if(len == 0) return "";
	var ch = str.slice(len-1);
	while(/(\s)|(\r\n)|(\r)|(\n)|(\f)|(\v)|(\t)/.test(ch)){
		str = str.slice(0,-1);
		len = str.length;
		if(len == 0) return "";
		ch = str.slice(len-1);
	}
	ch = str.slice(0,1);
	while(/(\s)|(\r\n)|(\r)|(\n)|(\f)|(\v)|(\t)/.test(ch)){
		if(str == "") return "";
		str = str.slice(1);
		ch = str.slice(0,1);
	}
	return str;
}
String.prototype.toPlainHTML = function(maxLen) {
	if (typeof(maxLen) == undefined) {
		maxLen = -1;
	}
	var s = this.trim();
	if (maxLen > -1) {
		s = s.substring(0, maxLen);
	}
	s = s.replace(/\n/, '-@####@-');
	s = s.replace(/ /, '-@#==#@-');
	var node = document.createElement("p");
	node.appendChild(document.createTextNode(s));
	s = node.innerHTML;
	s = s.replace('-@####@-', '<br/>');
	s = s.replace('-@#==#@-', '&nbsp;');
	return s;
}

function FormWatcher(formobj,submitfunc){
	this.form = $(formobj);
	this.callback = submitfunc;
	this.Ini = function(){
		this.form.bind('submit',this,this.submit);
		var ele = this.form.find("textarea");
		ele.bind('keypress',this,this.SubmitOnce);
	}
	this.SubmitOnce = function(evt){
	    var x = evt.keyCode;
		var q = evt.ctrlKey;
		if (q && ( x == 13 || x == 10)) {
			if(evt.data.callback()){
				evt.data.form.submit();
			}
		}
	}
	this.submit = function(evt){//在提交事件中先执行一个函数，然后在阻止提交事件。
		if(!evt.data.callback()){
			evt.preventDefault();
		}			
	}
	this.Ini();
}